package pac1_rgr;

import process.Actor;
import process.DispatcherFinishException;
import process.IWaitCondition;
import queues.QueueForTransactions;
import widgets.ChooseRandom;

public class Car extends Actor {
	protected int size;
	private Main gui;
	@SuppressWarnings("unused")
	private Model model;
	private int cnt;
	private ChooseRandom rnd;
	private QueueForTransactions queueOfCars;

	public Car(String name, Main gui, Model model) {
		this.setNameForProtocol(name);
		this.gui = gui;
		this.model = model;
		this.queueOfCars = model.getQueueOfCars();
		this.rnd = gui.getChooseRandomCar1();
	}

	@Override
	protected void rule() {
		final int sizeOfCar = 40;
		IWaitCondition morning = new IWaitCondition() {

			@Override
			public boolean testCondition() {
				return getDispatcher().getCurrentTime() % 24 == 8;
			}

			public String toString() {
				return "8 ранку";
			}
		};
		IWaitCondition evening = new IWaitCondition() {

			@Override
			public boolean testCondition() {
				return getDispatcher().getCurrentTime() % 24 == 17;
			}

			public String toString() {
				return "5 вечора";
			}
		};
		IWaitCondition full = new IWaitCondition() {

			@Override
			public boolean testCondition() {
				return cnt == sizeOfCar;
			}

			public String toString() {
				return "завантаження";
			}
		};
		while (true) {
			if (getDispatcher().getCurrentTime() >= gui
					.getChooseDataModelTime().getDouble()) {
				return;
			}
			try {
				waitForCondition(morning);
			} catch (DispatcherFinishException e) {
				return;
			}
			if (!full.testCondition()) {
				getDispatcher().printToProtocol(
						this.getNameForProtocol() + "поїхав на завантаження");
				waitForConditionOrHoldForTime(evening, rnd.next());
				if (evening.testCondition()) {
					continue;
				}
				queueOfCars.addLast(this);
				//gui.getHisto1().add(queueOfCars.size());
				//this.cnt = 0;
				try {
					waitForCondition(full);
				} catch (DispatcherFinishException e) {
					return;
				}
				queueOfCars.remove(this);
				//gui.getHisto1().add(queueOfCars.size());
			}
			getDispatcher().printToProtocol(
					this.getNameForProtocol() + "поїхав на розвантаження");
			waitForConditionOrHoldForTime(evening, rnd.next());
			if (evening.testCondition()) {
				continue;
			}
			this.cnt = 0;
		}
	}

	/*public void setSizeOfCar(int sizeOfCar) {
		this.sizeOfCar = sizeOfCar;
	}

	public int getSizeOfCar() {
		return sizeOfCar;
	}
*/

}
